package com.cda.controller;

import com.cda.common.BaseLogger;
import com.cda.entity.ListRecord;
import com.cda.entity.Person;
import com.cda.entity.SfFaceHeartBeatEntity;
import com.cda.entity.SfdevicebindEntity;
import com.cda.service.FaceService;
import com.cda.service.PosInputInfoSevice;
import com.cda.service.SfFaceHeartBeatService;
import com.cda.service.SfdevicebindService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;


@Controller
@RequestMapping("/face")
public class FaceController extends BaseLogger {

	@Autowired
	private SfdevicebindService sfdevicebindService;
	@Autowired
	FaceService faceService;
	@Autowired
	SfFaceHeartBeatService sfFaceHeartBeatService;
	@Autowired
	PosInputInfoSevice posInputInfoSevice;

	@RequestMapping(value="/photo",method = RequestMethod.GET)
	@ResponseBody
	public String photograph(){


		Person p = new Person();
		p.setId("001");//ID,验证人员唯一性的字段作
		p.setName("人脸识别成功");//人员的名字
		p.setIC_NO("1001");//为维根号发送到闸机
		p.setID_NO("440923199106284111");//	员工身份证号

		faceService.photograph(p);

		return null;
	}


	/**
	 * 		"personID": "10000",
	 * 				"ts": 1539666562.0,
	 * 				"passType": 0,//0:不启用,1:访客人证核验自动通行,2: 访客手动录入
	 * 				"name": "zhang san",
	 * 				"photo": "URLEncode(Base64(photo))",
	 * 				"visitor":true  //true-是访客，false-非访客，默认false
	 * @param   lr
	 * @return
	 */
	@RequestMapping(value="/listRecord",produces="application/x-www-form-urlencoded;charset=utf-8",method = RequestMethod.POST)
	@ResponseBody
	public String listRecord(ListRecord lr){

		logger.info("【执行解析人脸识别回调函数】"+JSONObject.fromObject(lr).toString());

		SfdevicebindEntity s =sfdevicebindService.findByClientIp(posInputInfoSevice.getHostIp());
		s.setName(lr.getName());
		s.setUpdateName(lr.getPassType()+"");
		posInputInfoSevice.opendDoor(lr.getPersonID()+lr.getTs()+"", null, s);
		return "{\n" +
				"\t\"msg\": \"成功\",\n" +
				"\t\"status\": 0\n" +
				"}";
	}



	/**
	 *  心跳接口配置心跳URL
	 * @param heartBeat
	 * @return
	 */
	@RequestMapping(value="/heartBeat",produces="application/x-www-form-urlencoded;charset=utf-8",method = RequestMethod.POST)
	@ResponseBody
	public String heartBeat(SfFaceHeartBeatEntity heartBeat){

		logger.info("【执行人脸设备心跳接口】"+JSONObject.fromObject(heartBeat).toString());
		try {
			sfFaceHeartBeatService.save(heartBeat);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "{\n" +
				"\t\"msg\": \"成功\",\n" +
				"\t\"status\": 0\n" +
				"}";
	}



}